# -*- coding: utf-8 -*-
"""
author:码同学 极光
date:2024/3/24
desc:
sample: 
"""
import re
import requests
from bs4 import BeautifulSoup


def baidu():
    res = requests.get(url='http://www.baidu.com/')
    print(res.text)
    res.encoding = 'utf-8'
    r = r"<title>(.*?)</title>"
    print(re.findall(r, res.text))
    # <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a>
    # <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a>
    r = r'<a href=(.+?) name=.+? class=mnav>(.+?)</a>'
    result = re.findall(r, res.text)
    print(result)
    #方案2
# soup = BeautifulSoup(res.text, 'html.parser')
# for a_tag in soup.find_all('a'):
#     href = a_tag.get('href')
#     print(a_tag.text)
#     print(href)


if __name__ == '__main__':
    baidu()
    # 正则 匹配 提取数据
    # r = r"\d+"
    # m = re.search(r, "a123test")
    # print(m)
    # print(re.findall(r, "a123test"))
    # 分组  .*  .+  默认是最大匹配 贪婪 ？
    str = 'a123ba456bab'
    print(re.findall('a(.+)b', str))
    print(re.findall('a(.*)b', str))
    print(re.findall('a(.+?)b', str))
    print(re.findall('a(.*?)b', str))
    str = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"' \
          ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">' \
          '<soap:Body><getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">' \
          '<getMobileCodeInfoResult>18810733325：北京 北京 北京移动全球通卡' \
          '</getMobileCodeInfoResult></getMobileCodeInfoResponse></soap:Body></soap:Envelope>'
    r = r'<getMobileCodeInfoResult>(.*)</getMobileCodeInfoResult>'
    result = re.findall(r, str)[0]
    print(result)
    # res = result.split('：')
    # text = "18810733325：北京 北京 北京移动全球通卡"

    res = result.split("：")
    print(res[0])
    print(res[1])

    # 多分组
    pattern = r'(\d{11})：(.*)'
    res = re.findall(pattern, result)
    print(res)
    for k, v in res:
        print(k, v)

    # 正则另外写法
    print('正则的match')
    match = re.match(pattern, result)
    if match:
        print(match.group(0))
        print(match.group(1))
        print(match.group(2))
